<?php
namespace app\blog\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
class Article extends Admin
{
    public function index(){
        $order=$this->getOrder();
        $data=$this->getMap();
        $datas=db("blog_article")->where($data)->order($order)->select();
        //将作者id和类别id查找替换为汉字
        $writer=[];
        $category=[];
        for ($i=0;$i<count($datas);$i++){
            $wid=$datas[$i]['wid'];
            $cid=$datas[$i]['cid'];
            $writer[]=db('blog_writer')->where('id='.$wid)->value('nikname');
            $category[]=db('blog_category')->where('id='.$cid)->value('name');
            $datas[$i]['wid']=$writer[$i];
            $datas[$i]['cid']=$category[$i];
        }
        return ZBuilder::make('table')
        ->addColumns([
            ['id','ID'],
            ['title','标题'],
            ['cid','所属分类'],
            ['wid','作者昵称'],
            ['content','内容'],
            ['createtime','创建时间','datetime'],
            ['right_button','操作','btn']
        ])
        ->addOrder('id,title,cid,wid,createtime')
        ->addFilter('id,title,content')
        ->setRowList($datas)
        ->addTopButtons('add,delete')
        ->addRightButtons('edit,delete')
        ->setSearch(['title'=>'标题','content'=>'内容'],'','',true)
        ->fetch();
    }
    public function add(){
        if ($this->request->isPost()){
            $data=$this->request->post();
            $data_list=[
                'id'=>'',
                'wid'=>$data['wid'],
                'cid'=>$data['cid'],
                'title'=>$data['title'],
                'content'=>$data['content'],
                'createtime'=>time(),
                'count'=>0
            ];
            $rs=db('blog_article')->insert($data_list);
            if ($rs){
                $this->success('添加成功','index');
            }else{
                $this->error('添加失败');
            }
        }
        //获得分类标题
        $categorys=db('blog_category')->order('id desc')->select();
        foreach ($categorys as $k=>$v){
            $k=$v['id'];
            $category[$k]=$v['name'];
        }
        //获得作者昵称
        $writers=db('blog_writer')->order('id desc')->select();
        foreach ($writers as $k=>$v){
            $k=$v['id'];
            $w[$k]=$v['nikname'];
        }
        return ZBuilder::make('form')
        ->addSelect('wid','选择作者','请选择作者',$w)
        ->addFormItems([
            ['hidden','id'],
            ['text','title','标题'],
            ['ckeditor','content','内容']
        ])
        ->addRadio('cid','选择文章类别','',$category)
        ->fetch();
    }
    public function edit($id=null){
        if ($this->request->isPost()){
            $update=$this->request->post();
            $rs=db('blog_article')->update($update);
            if ($rs){
                $this->success('修改成功','index');
            }else{
                $this->error('修改失败');
            }
        }
        $data=db('blog_article')->where('id='.$id)->find();
        //将作者id换为作者昵称
        $writer=db('blog_writer')->where('id',$data['wid'])->value('nikname');
        $data['wid']=$writer;
        //将类别id换为类别名称
        $categorys=db('blog_category')->order('id desc')->select();
        foreach ($categorys as $k=>$v){
            $k=$v['id'];
            $category[$k]=$v['name'];
        }
        //得到所有作者昵称做下拉框
        $writers=db('blog_writer')->order('id desc')->select();
        foreach ($writers as $k=>$v){
            $k=$v['id'];
            $w[$k]=$v['nikname'];
        }
        return ZBuilder::make('form')
        ->addSelect('wid','选择作者','请选择作者',$w,$data['wid'])
        ->addFormItems([
            ['hidden','id'],
            ['text','title','标题'],
            ['ckeditor','content','内容']
        ])
        ->addRadio('cid','选择文章类别','',$category)
        ->fetch('',$data);
    }
    public function delete($ids=null){
        if($ids){
            $rs1=db('blog_article')->where('id',$ids)->delete();
            $rs2=db('blog_pic')->where('aid',$ids)->delete();
            if ($rs1 && $rs2){
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }
    }
    public function search(){
        //获取筛选
        $data=$this->getMap();
        $data_list=db('blog_article')->where($data)->order('id desc')->select();
        $writer=[];
        $category=[];
        for ($i=0;$i<count($data_list);$i++){
            $wid=$data_list[$i]['wid'];
            $cid=$data_list[$i]['cid'];
            $writer[]=db('blog_writer')->where('id='.$wid)->value('nikname');
            $category[]=db('blog_category')->where('id='.$cid)->value('name');
            $data_list[$i]['wid']=$writer[$i];
            $data_list[$i]['cid']=$category[$i];
        }
        return ZBuilder::make('table')
        ->addColumns([
            ['id','ID'],
            ['title','标题'],
            ['cid','所属分类'],
            ['wid','作者昵称'],
            ['content','内容'],
            ['createtime','创建时间','datetime'],
            ['right_button','操作','btn']
        ])
        ->setRowList($data_list)
        ->addTopButtons('add,delete')
        ->addRightButtons('edit,delete')
        ->setSearch(['title'=>'标题','content'=>'内容'],'','',true)
        ->fetch();
    }
}

?>